package com.qiaoanju.web.back.controller.system;

import com.qiaoanju.back.domain.system.DictModel;
import com.qiaoanju.back.domain.system.UserModel;
import com.qiaoanju.back.service.system.IDictService;
import com.qiaoanju.commons.constants.ErrorCodes;
import com.qiaoanju.commons.constants.SessionConstant;
import com.qiaoanju.commons.constants.StringConstant;
import com.qiaoanju.commons.enums.ResultCode;
import com.qiaoanju.commons.utils.DateUtil;
import com.qiaoanju.commons.utils.StringUtil;
import com.qiaoanju.web.controller.BaseController;
import com.qiaoanju.web.security.service.LoginUserService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 * Created by eversec on 2017/10/21.
 */
@Controller
@RequestMapping("/sysdict")
public class DictController extends BaseController {


    @Autowired
    private IDictService service ;

    @RequestMapping(value = "/index", method = { RequestMethod.GET })
    public String index(Model model, HttpServletRequest request,
                        HttpServletResponse response) throws Exception {
        //获取登录用户的信息
        model.addAttribute(SessionConstant.SESSION_USER, LoginUserService.getLoginUser());
        Map<String ,Object> params = new HashMap();
        List<DictModel> list = service.getDictsList(params);
        model.addAttribute("list",list);
        return "sys/dict/list";
    }

    @RequestMapping(value = "/insert", method = { RequestMethod.POST})
    @ResponseBody
    public void add(Model model, HttpServletRequest request,
                    HttpServletResponse response, DictModel dict) throws Exception {

        UserModel login = LoginUserService.getLoginUser() ;
        dict.setCreate_id(login.getId());
        dict.setUpdate_id(login.getId());
        dict.setUpdate_time(DateUtil.getCurrentDate());

        int result = service.insert(dict);
        //处理结果
        if (result > 0){
            printJson(response, ResultCode.Success, StringConstant.INSERT_MSG);
        }else{
            printJson(response,ResultCode.Fail, StringConstant.OPERATE_FAILURE_MSG);
        }
    }


    @RequestMapping(value = "/deleteByIds", method = { RequestMethod.POST})
    @ResponseBody
    public void deleteById(Model model, HttpServletRequest request,
                    HttpServletResponse response, String ids){
        int result = 0 ;
        if(!StringUtils.isBlank(ids)){
            int[] idArr = StringUtil.getIntArryFromString(ids,",");
            if(idArr.length > 1){
                result = service.deleteByIds(idArr);
            }else{
                result = service.deleteById(idArr[0]);
            }

            //处理结果
            if (result > 0){
                printJson(response, ResultCode.Success, StringConstant.DELETE_MSG);
            }else{
                printJson(response,ResultCode.Fail, StringConstant.OPERATE_FAILURE_MSG);
            }
        }else{
            printJson(response,ErrorCodes.PARAM_NULL, StringConstant.PARAM_NULL_MSG);
        }

    }


    @RequestMapping(value = "/editModel", method = { RequestMethod.GET})
    @ResponseBody
    public void editModel(Model model, HttpServletRequest request,
                           HttpServletResponse response, int id){
        int result = 0 ;
        if(id != 0){
            DictModel dict = service.getDictById(id);
            //处理结果
            if (dict != null){
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("data", dict);
                jsonObject.put("msg", StringConstant.FORMAT_DATA_SUCCESS_MSG);
                printJson(response,ResultCode.Success,jsonObject);
            }else{
                printJson(response,ErrorCodes.PARAM_ERROR, StringConstant.PARAM_ERROR_MSG);
            }
        }else{
            printJson(response,ErrorCodes.PARAM_NULL, StringConstant.PARAM_NULL_MSG);
        }

    }


    @RequestMapping(value = "/edit", method = { RequestMethod.POST})
    @ResponseBody
    public void edit(Model model, HttpServletRequest request,
                          HttpServletResponse response, DictModel dict){
        boolean result = false;
        if(dict != null && dict.getId() != 0){
            DictModel editModel = service.getDictById(dict.getId());

            editModel.setPid(dict.getPid());
            editModel.setItem_key(dict.getItem_key());
            editModel.setItem_value(dict.getItem_value());
            editModel.setUpdate_id(LoginUserService.getLoginUserId());
            editModel.setUpdate_time(DateUtil.getCurrentDate());
            result = service.edit(editModel);
            //处理结果
            if (result){
                printJson(response, ResultCode.Success, StringConstant.UPDATE_MSG);
            }else{
                printJson(response,ResultCode.Fail, StringConstant.OPERATE_FAILURE_MSG);
            }
        }else{
            printJson(response,ErrorCodes.PARAM_ERROR, StringConstant.PARAM_ERROR_MSG);
        }

    }
}
